package com.cuixichang.minimalism.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cuixichang.minimalism.core.mybatis.cnstants.BaseFieldNames;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import com.cuixichang.minimalism.mapper.IRouterRelationMapper;
import com.cuixichang.minimalism.transfer.dto.IRouterRelationDTO;
import com.cuixichang.minimalism.transfer.factory.IRouterRelationAssembler;
import com.cuixichang.minimalism.transfer.po.IRouterRelation;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.time.LocalDateTime;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import java.util.function.Function;
import org.springframework.stereotype.Service;

/**
 * 系统路由绑定关系表表服务实现类
 *
 * @author cuixichang
 * @date 2024-12-18
 */
@Service
public class IRouterRelationServiceImpl implements IRouterRelationService {

    @Autowired
    private IRouterRelationMapper iRouterRelationMapper;

    @Override
    public IRouterRelationDTO selectById(Long id){
        IRouterRelation iRouterRelation = iRouterRelationMapper.selectById(id);
        return IRouterRelationAssembler.INSTANCT.convertDto(iRouterRelation);
    }

    @Override
    public IRouterRelationDTO selectOne(IRouterRelationDTO iRouterRelationDTO){
        Assert.notNull(iRouterRelationDTO,"参数为空,请检查");
        IRouterRelation iRouterRelation = IRouterRelationAssembler.INSTANCT.convertPo(iRouterRelationDTO);
        IRouterRelation result = iRouterRelationMapper.selectOne(new QueryWrapper<>(iRouterRelation));
        return IRouterRelationAssembler.INSTANCT.convertDto(result);
    }

    @Override
    public List<IRouterRelationDTO> selectByIds(List<Long> ids) {
        Assert.notEmpty(ids,"参数为空,请检查");
        List<IRouterRelation> results = iRouterRelationMapper.selectBatchIds(ids);
        return results.stream().map(IRouterRelationAssembler.INSTANCT::convertDto).collect(Collectors.toList());
    }

    @Override
    public List<IRouterRelationDTO> selectList(IRouterRelationDTO iRouterRelationDTO){
        return this.selectList(iRouterRelationDTO, (iRouterRelation, queryWrapper) -> {});
    }

    @Override
    public List<IRouterRelationDTO> selectList(IRouterRelationDTO iRouterRelationDTO, BiConsumer<IRouterRelation, QueryWrapper<IRouterRelation>> consumer){
        Assert.notNull(iRouterRelationDTO,"参数为空,请检查");
        IRouterRelation iRouterRelation = IRouterRelationAssembler.INSTANCT.convertPo(iRouterRelationDTO);
        QueryWrapper<IRouterRelation> queryWrapper = new QueryWrapper<>(iRouterRelation);
        if(Objects.nonNull(consumer)){
            consumer.accept(iRouterRelation, queryWrapper);
        }
        queryWrapper.orderBy(true,true,BaseFieldNames.SORT).orderBy(true,false,BaseFieldNames.UPDATE_TIME);
        List<IRouterRelation> results = iRouterRelationMapper.selectList(queryWrapper);
        return results.stream().map(IRouterRelationAssembler.INSTANCT::convertDto).collect(Collectors.toList());
    }

    @Override
    public Page<IRouterRelationDTO> selectPage(IRouterRelationDTO iRouterRelationDTO,long pageSize,long pageNo){
        return this.selectPage(iRouterRelationDTO, pageSize, pageNo, (iRouterRelation, queryWrapper) -> {});
    }

    @Override
    public Page<IRouterRelationDTO> selectPage(IRouterRelationDTO iRouterRelationDTO,long pageSize,long pageNo, BiConsumer<IRouterRelation, QueryWrapper<IRouterRelation>> consumer){
        Assert.notNull(iRouterRelationDTO,"参数为空,请检查");
        IRouterRelation iRouterRelation = IRouterRelationAssembler.INSTANCT.convertPo(iRouterRelationDTO);
        QueryWrapper<IRouterRelation> queryWrapper = new QueryWrapper<>(iRouterRelation);
        if(Objects.nonNull(consumer)) consumer.accept(iRouterRelation, queryWrapper);
        queryWrapper.orderBy(true,true,BaseFieldNames.SORT).orderBy(true,false,BaseFieldNames.UPDATE_TIME);
        Page<IRouterRelation> page = iRouterRelationMapper.selectPage(new Page<>(pageNo,pageSize),queryWrapper);
        Page<IRouterRelationDTO> result = Page.of(page.getCurrent(),page.getSize(),page.getTotal());
        result.setRecords(page.getRecords().stream()
                .map(IRouterRelationAssembler.INSTANCT::convertDto).collect(Collectors.toList()));
        return result;
    }

    @Override
    public int deleteById(Long id){
        return iRouterRelationMapper.deleteById(id);
    }

    @Override
    public int deleteByIds(List<Long> ids){
        return iRouterRelationMapper.deleteBatchIds(ids);
    }

    @Override
    public int deleteByCondition(IRouterRelationDTO iRouterRelationDTO){
        return deleteByCondition(iRouterRelationDTO, (iRouterRelation, queryWrapper)-> {});
    }

    @Override
    public int deleteByCondition(IRouterRelationDTO iRouterRelationDTO, BiConsumer<IRouterRelation, QueryWrapper<IRouterRelation>> consumer){
        Assert.notNull(iRouterRelationDTO,"参数为空,请检查");
        IRouterRelation iRouterRelation = IRouterRelationAssembler.INSTANCT.convertPo(iRouterRelationDTO);
        QueryWrapper<IRouterRelation> queryWrapper = new QueryWrapper<>(iRouterRelation);
        if(Objects.nonNull(consumer)) {
            consumer.accept(iRouterRelation, queryWrapper);
        }
        return iRouterRelationMapper.delete(queryWrapper);
    }

    @Override
    public IRouterRelationDTO insert(IRouterRelationDTO iRouterRelationDTO){
        return insertHasOption(iRouterRelationDTO, (IRouterRelationDTO) -> null);
    }

    @Override
    public IRouterRelationDTO insertHasOption(IRouterRelationDTO iRouterRelationDTO, Function<IRouterRelationDTO,IRouterRelationDTO> preOption){
        Assert.notNull(iRouterRelationDTO,"参数为空,请检查");
        iRouterRelationDTO.check();
        IRouterRelation iRouterRelation = IRouterRelationAssembler.INSTANCT.convertPo(iRouterRelationDTO);
        Long createLoginId = iRouterRelation.getCreateLoginId();
        Assert.notNull(createLoginId,"创建人ID为空,请检查");
        iRouterRelation.setUpdateLoginId(createLoginId);
        LocalDateTime localDateTime = LocalDateTime.now();
        iRouterRelation.setCreateTime(localDateTime);
        iRouterRelation.setUpdateTime(localDateTime);
        Boolean isEnable = iRouterRelation.getIsEnable();
        if(Objects.isNull(isEnable)) iRouterRelation.setIsEnable(true);
        Integer sort = iRouterRelation.getSort();
        if(Objects.isNull(sort)) iRouterRelation.setSort(0);
        if(Objects.nonNull(preOption)){
            IRouterRelationDTO apply = preOption.apply(iRouterRelationDTO);
            if(Objects.nonNull(apply)) {
              return apply;
            }
        }
        int insert = iRouterRelationMapper.insert(iRouterRelation);
        Assert.isTrue(insert>0,"插入失败");
        return IRouterRelationAssembler.INSTANCT.convertDto(iRouterRelation);
    }

    @Override
    public int updateById(IRouterRelationDTO iRouterRelationDTO){
         return updateById(iRouterRelationDTO, (iRouterRelation, updateWrapper) -> {});
    }

    @Override
    public int updateById(IRouterRelationDTO iRouterRelationDTO, BiConsumer<IRouterRelation, UpdateWrapper<IRouterRelation>> consumer){
        Assert.notNull(iRouterRelationDTO,"参数为空,请检查");
        Long id = iRouterRelationDTO.getId();
        Assert.notNull(id,"主键为空,请检查");
        IRouterRelation iRouterRelation = IRouterRelationAssembler.INSTANCT.convertPo(iRouterRelationDTO);
        Long createLoginId = iRouterRelation.getCreateLoginId();
        Assert.notNull(createLoginId,"创建人ID为空,请检查");
        iRouterRelation.setCreateLoginId(null);
        iRouterRelation.setUpdateLoginId(createLoginId);
        iRouterRelation.setUpdateTime(LocalDateTime.now());
        UpdateWrapper<IRouterRelation> updateWrapper = new UpdateWrapper<IRouterRelation>().eq(BaseFieldNames.ID, id);
        if(Objects.nonNull(consumer)) consumer.accept(iRouterRelation,updateWrapper);
        return iRouterRelationMapper.update(iRouterRelation,updateWrapper);
    }
}
